package com.sgb.jicai.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.jicai.databinding.ItemParticipationOfferSecondCategoryBinding;
import com.sgb.link.utils.JiCaiEditInputFilter;
import com.swgk.core.BR;
import com.swgk.core.util.TimeUtil;
import com.sgb.jicai.entity.OfferGroupBuyingDetailsEntity;
import com.sgb.link.utils.UnitListUtils;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ParticipationOfferSecondAdapter extends BaseRecycleAdapter2<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> {

    private int type;  // 1.参与报价   2.修改报价
    private Calendar selectedDate = Calendar.getInstance();
    private Calendar dayCalendar = Calendar.getInstance();
    private ItemCallBack itemCallBack;
    private long deadline; // 最后供应时间
    private boolean add;
    private String groupBuyType;
    private String unitNames;

    /**
     * @param context
     * @param layoutId
     * @param deadline
     */
    public ParticipationOfferSecondAdapter(Context context, int layoutId, long deadline,String groupBuyType,String unitName) {
        super(context, layoutId, null);
        this.context = context;
        this.deadline = deadline;
        this.groupBuyType = groupBuyType;
        this.unitNames = unitName;
    }



    @Override
    public void convert(ItemViewHolder holder, OfferGroupBuyingDetailsEntity.GroupBuyingDetails entity, int position) {
        holder.setBinding(BR.viewModel, entity);
        //小数点后保留两位数字
        if (UnitListUtils.getUnitStart(unitNames)){
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvStock.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999.999, 0.001,3)});
        }else {
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvStock.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999, 1,0)});
        }
        ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvPrice.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999999.99, 0.01,2)});
        ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvSort.setText(String.valueOf(position + 1));
        entity.setSort(((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvSort.getText().toString());
        if (getItemCount() > 1) {
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).llSortView.setVisibility(View.VISIBLE);
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).lineView.setVisibility(View.VISIBLE);
        } else {
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).llSortView.setVisibility(View.GONE);
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).lineView.setVisibility(View.GONE);
        }
        ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvStatTime.setOnClickListener(view ->
                showCourseTimePop(((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvStatTime, entity,1));
        ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvEndTime.setOnClickListener(view ->
                showCourseTimePop(((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).tvEndTime, entity,2));
        if (entity.getType() == 1) {
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).arrowRightDelete.setVisibility(View.VISIBLE);
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).arrowRightDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemCallBack != null) {
                        itemCallBack.goPriceRole(entity, position);
                    }
                }
            });
        } else {
            ((ItemParticipationOfferSecondCategoryBinding) holder.getmBinding()).arrowRightDelete.setVisibility(View.GONE);
        }
    }

    //时间选择
    private void showCourseTimePop(TextView tvTime, OfferGroupBuyingDetailsEntity.GroupBuyingDetails entity,int Type) {
        if (deadline < 100) {//时间戳
            dayCalendar.setTime(new Date());
        } else {
            dayCalendar.setTime(TimeUtil.getDateFromFormatString(TimeUtil.getDateStringYMD(deadline)));//非商品的入场时间和结束时间需在报价截止时间后
        }
        dayCalendar.add(Calendar.DAY_OF_MONTH, 1);
        TimePickerView timePicker = new TimePickerBuilder(context, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            try {
                if (Type==1){//开始时间选择
                    if (!TextUtils.isEmpty(entity.getSupplyTime())&&Long.parseLong(entity.getSupplyTime().replace("-","")) < Long.parseLong(format.replace("-",""))) {
                        MToast.showToast(context, "开始时间不能大于结束时间");
                        return;
                    }
                    if (!entity.getSupplyTime().equals(format)){
                        entity.setSupplyTimeStart(String.valueOf(TimeUtil.getLongTime(format, "yyyy-MM-dd")));
                    }else {
                        MToast.showToast(context, "开始时间不能和结束时间为同一天");
                        return;
                    }
                }else {
                    if (TextUtils.isEmpty(entity.getSupplyTimeStart())) {
                        MToast.showToast(context, "请先选择开始时间！");
                        return;
                    }
                    //结束时间选择
                    if (!TextUtils.isEmpty(entity.getSupplyTimeStart())&&Long.parseLong(entity.getSupplyTimeStart().replace("-","")) >= Long.parseLong(format.replace("-",""))) {
                        MToast.showToast(context, "结束时间不能小于开始时间");
                        return;
                    }
                    if (!entity.getSupplyTimeStart().equals(format)){
                        entity.setSupplyTime(String.valueOf(TimeUtil.getLongTime(format, "yyyy-MM-dd")));
                    }else {
                        MToast.showToast(context, "结束时间不能和开始时间为同一天");
                        return;
                    }
                }
                tvTime.setText(format);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(dayCalendar, null)
//                .setRangDate(null, null)//不限起始和结束时间
                .build();
        timePicker.show();
      //  timePicker.setDate(selectedDate);
    }

    public void setOfferEndtime(long deadline) {
        this.deadline = deadline;
    }

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }
    public interface ItemCallBack {
        void goPriceRole(OfferGroupBuyingDetailsEntity.GroupBuyingDetails entity, int index);

    }
}
